package com.example.servlet;

import com.example.entity.customerAdmin.Follow;
import com.example.service.FollowService;
import com.example.utils.BaseServlet;
import com.example.utils.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/follow")
public class FollowServlet extends BaseServlet {
    FollowService followService=new FollowService ();
    @Override
    public Class getChildClass () {
        return FollowServlet.class;
    }

    public ResultData addFollow(HttpServletRequest request, HttpServletResponse response){
        String type = request.getParameter ("type");
        String mode = request.getParameter ("mode");
        String remarks = request.getParameter ("followContent");
        String name = request.getParameter ("cusName");
        String custatus = request.getParameter ("custatus");
        String contactUser = request.getParameter ("contactUser");
        String who = request.getParameter ("who");
        String followTime = request.getParameter ("followTime");
        Follow follow=new Follow ();
        follow.setType (type);
        follow.setMode (mode);
        follow.setRemarks (remarks);
        follow.setCusName (name);
        follow.setCusType (custatus);
        follow.setContactUser (contactUser);
        follow.setRemindWho (who);
        follow.setTime (followTime);
        int i = followService.addFollow (follow);
        ResultData resultData=new ResultData();
        if(i>0){
            resultData.setCode (200);
        }else {
            resultData.setCode (404);
        }

        return resultData;
    }
    public ResultData del(HttpServletRequest request, HttpServletResponse response){
        int id =Integer.parseInt (request.getParameter ("id")) ;
        int del = followService.del (id);
        ResultData resultData=new ResultData ();
        if(del>0){
            resultData.setCode (200);

        }else {
            resultData.setCode (404);
        }

        return resultData;

    }
    public ResultData selectAll(HttpServletRequest request, HttpServletResponse response){
        List<Follow> list = followService.selectAll ();
        HttpSession session = request.getSession ();
        session.setAttribute ("list",list);
        ResultData resultData=new ResultData ();
        resultData.setData (list);
        resultData.setCode (200);
        return resultData;
    }
    public ResultData selectById(HttpServletRequest request, HttpServletResponse response){
        List<Follow> follows=new ArrayList<> ();
        int id =Integer.parseInt (request.getParameter ("id"));
        Follow follow = followService.selectById (id);
        follows.add (follow);
        ResultData resultData=new ResultData ();
        resultData.setCode (200);
        resultData.setData (follows);
        return resultData;
    }
   /* public String selectFollowBy(HttpServletRequest request, HttpServletResponse response){
        int id =Integer.parseInt (request.getParameter ("id"));
        Follow follow = followService.selectById (id);
        HttpSession session = request.getSession ();
        session.setAttribute ("follow",follow);
        return "/admin/page/system/user/followDetail";
    }*/
}

